﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BS.MVC;
using EGym.BL.DataGestor;
using EGym.BL.Forms;
using EGym.Data;
using EGym.Models;

namespace EGym.Controllers
{
    public partial class AlunoController
    {
        //
        // GET: /Aluno/

        public ActionResult Search(string text)
        {
            if (SessionManager.EstaAutenticado())
            {
                AlunoGestor gestor = new AlunoGestor(new DBDataContext());
                var lista = gestor.GetAlunosPaginedListByNome(SessionManager.Usuario.AfiliadoId,
                                                               text,
                                                               Constantes.PAGE_SIZE,
                                                               Constantes.PAGE_INDEX_STARTUP);

                return Json(from reg in lista
                            select new
                            {
                                Nome = reg.Nome,
                                AlunoId = reg.AlunoId,
                                DataCadastro = reg.DataCadastro.ToString("dd/MM/yyy"),
                                DataNascimento = reg.DataNascimento.ToString("dd/MM/yyy"),
                                Email = reg.Email
                            }

                                   );
            }
            return Json(null);
        }

        public JsonResult GetJsonResultById(string idText)
        {
            idText = idText ?? string.Empty;

            string[] ids = idText.SplitByCota();
            AlunoGestor gestor = new AlunoGestor(new DBDataContext());
            var reg = gestor.GetAlunoById(Convert.ToInt32(ids[0]),
                                            Convert.ToInt32(ids[1]));

            return Json(new
                            {
                                Nome = reg.Nome,
                                AfiliadoId = reg.AfiliadoId,
                                AlunoId = reg.AlunoId,
                                DataCadastro = reg.DataCadastro.ToString("dd/MM/yyy"),
                                DataNascimento = reg.DataNascimento.ToString("dd/MM/yyy"),
                                Email = reg.Email,
                                TwitterAccount = reg.TwitterAccount
                        });

            //return Json(reg);
        }

    }
}
